<template>
    <div>
            <Card>
                <p slot="title">
				    企业认证审核
			    </p>
                <div>
                    <Form class="step-form" ref="userData" :model="userData" :rules="userDataRules"  :label-width="100">
                       <Row span="12">
                            <i-col span="12" >
                                <FormItem label="企业名称：" prop="name" label-position="left" :label-width="150">
                                    <Input  :disabled="hasSubmit" v-model="userData.name" type="text" placeholder="输入数据不能为空" />
                                </FormItem>
                            </i-col>
                        </Row>
                        <Row span="12">
                            <i-col span="12" >
                                <FormItem label="法人姓名：" prop="legalPerson" label-position="left" :label-width="150">
                                    <Input :disabled="hasSubmit" v-model="userData.legalPerson" type="text" placeholder="输入数据不能为空" />
                                </FormItem>
                            </i-col>
                        </Row>
                        <Row span="12">
                            <i-col span="12" >
                                <FormItem label="法人身份证号：" prop="legalCard" label-position="left" :label-width="150">
                                    <Input :disabled="hasSubmit" v-model="userData.legalCard" type="text" placeholder="输入数据不能为空" />
                                </FormItem>
                            </i-col>
                        </Row>
                        <Row span="12">
                            <i-col span="12" >
                                <FormItem label="手机号：" prop="legalPhone" label-position="left" :label-width="150">
                                    <Input :disabled="hasSubmit" v-model="userData.legalPhone" type="text" placeholder="手机号不能为空" />
                                </FormItem>
                            </i-col>
                        </Row>
                        <Row span="12">
                            <i-col span="12" >
                                <FormItem label="营业执照号：" prop="businessLicense" label-position="left" :label-width="150">
                                    <Input :disabled="hasSubmit" v-model="userData.businessLicense" type="text" placeholder="营业执照号不能为空" />
                                </FormItem>
                            </i-col>
                        </Row>
                         <Row span="12">
                            <i-col span="10">
                                <FormItem label="营业执照图片：" prop="businessLicenseUrl" label-position="left" :label-width="150">
                                    <img v-if="userData.businessLicenseUrl !== ''" :src="userData.businessLicenseUrl" height="200" width="200">
                                </FormItem>
                            </i-col>
                            <i-col span="1">
                                <Upload
                                    :show-upload-list="false"
                                    :before-upload="handleUpload"
                                    action="//jsonplaceholder.typicode.com/posts/">
                                    <Button>上传</Button>
                                </Upload>
                            </i-col>
                        </Row>
                        <Row span="12">
                            <i-col span="10">
                                <FormItem label="门头照：" prop="doorHeaderUrl" label-position="left" :label-width="150">
                                    <img v-if="userData.doorHeaderUrl !== ''" :src="userData.doorHeaderUrl" height="200" width="200">
                                </FormItem>
                            </i-col>
                            <i-col span="1">
                                <Upload
                                    :show-upload-list="false"
                                    :before-upload="handleUpload2"
                                    action="//jsonplaceholder.typicode.com/posts/">
                                    <Button>上传</Button>
                                </Upload>
                            </i-col>
                        </Row>
                        
                        <FormItem >
                            <Button @click="saveData" style="width:100px;" type="primary">确定</Button>
                            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                            <Button @click="goBack" style="width:100px;" >返回</Button>
                        </FormItem>
                    </Form>
                </div>
            </Card>
            
    </div>
</template>


<script>
import ajax from '../../../libs/axios.js'
export default {
    name: 'addUser',
    data () {
        return {
            modal1:false,
            testModelShow:false,
            symptomList:[],
            selectModel:[],
            userData: {
                id:'',
                name:'',
                legalPerson:'',
                legalCard:'',
                legalPhone: '',
                businessLicense:'',
                businessLicenseUrl:'',
                doorHeaderUrl:''
            },
            hasSubmit: false,
            // 图片上传
            file: null,
            status: 'wait',
            uploadLoading: false, // 上传图片时候的加载效果,
            failreason:"",//审核失败
            userId:-1,//当前登陆人员的Id
            userName:"",//当前登陆人员的姓名
            id:-1,
            userDataRules: {
                name: [
                    { required: true, message: '请输入企业名称', trigger: 'blur'},
                    {type: 'string', max: 15, message: '不能超过15个字符', trigger: 'blur'}
                ],
                legalPerson: [
                    { required: true, message: '请输入法人名称', trigger: 'blur'},
                    {type: 'string', max: 15, message: '不能超过15个字符', trigger: 'blur'}
                ],
                legalCard: [
                    { required: true, message: '请输入法人身份证号', trigger: 'blur'},
                    {type: 'string', max: 18, message: '请输入正确格式的身份证号码', trigger: 'blur'},
                    {type: 'string', min: 18, message: '请输入正确格式的身份证号码', trigger: 'blur'}
                ],
                legalPhone: [
                    { required: true, message: '请输入手机号', trigger: 'blur'},
                    {type: 'string', max: 11, message: '手机号格式不正确', trigger: 'blur'},
                    {type: 'string', min: 11, message: '手机号格式不正确', trigger: 'blur'}
                ],
                businessLicense: [
                    { required: true, message: '请输入营业执照号码', trigger: 'blur'}
                ],
                // 暂时不进行验证
                // businessLicenseUrl: [
                //     { required: true, message: '请上传营业执照图片', trigger: 'blur'},
                // ],
                // doorHeaderUrl: [
                //     { required: true, message: '请上传门头照', trigger: 'blur'},
                // ]
                
            },
        }
    },
    methods: {
        // 数据初始化
        init () {
            this.userId = window.localStorage.getItem('userId')
            this.userName = window.localStorage.getItem('userName')
            this.$store.commit('setActiveName', 'partyindex');
            var id = this.$route.params.id;
            this.partyInfo(id);
        },
        partyInfo(id) {
            var _that = this;
            var url = 'adminlogin/selectCjPartyById';
            var data = {
                id: id
            };
            ajax.formRes(url, data).then(function (res) {
                if (200 == res.code) {
                    _that.userData = res.data;
                } else {
                    _that.$Message.success('数据原因，请联系管理员。');
                }
            }).catch(function (res) {
                console.log('有错误信息');
            });
        },
        // 营业执照图片
        handleUpload (file) {
            this.file = file
            this.upload(1)
            return false;
        },
        //门头照图片
        handleUpload2 (file) {
            this.file = file
            this.upload(2)
            return false;
        },
        // 上传图片
        upload (type) {
            let that = this
            let fileName = that.file.name
            //FormData 对象
            let formData = new FormData()
            console.log(fileName)
            this.uploadLoading = true
            // 文件对象
            formData.append("img", that.file)
            ajax.upload('adminlogin/file/uploadOssImage',formData).then(function (res) {
                if(type == 1){
                    setTimeout(() => {
                        that.userData.businessLicenseUrl = res.url
                        that.file = ''
                        that.$Message.success('上传成功')
                    }, 500);
                }else if(type == 2){
                    setTimeout(() => {
                        that.userData.doorHeaderUrl = res.url
                        that.file = ''
                        that.$Message.success('上传成功')
                    }, 500);
                }else{
                     that.$Message.success('上传失败，没有该类型数据')
                }                
                //that.uploadLoading = false
            }, function (err) {
                console.log(err)
                //this.uploadLoading = false
            }).catch(function (error) {
                console.log(error)
                //this.uploadLoading = false
            })
        },
        // 添加数据
        saveData () {
            let that = this
            that.$refs['userData'].validate((valid) => {
                if (valid) {
                    that.hasSubmit = true
                    let url = 'adminlogin/updateCjParty'
                    let data = {
                        id:that.userData.id,
                        name:that.userData.name,
                        legalPerson:that.userData.legalPerson,
                        legalCard:that.userData.legalCard,
                        legalPhone: that.userData.legalPhone,
                        businessLicense:that.userData.businessLicense,
                        businessLicenseUrl:that.userData.businessLicenseUrl,
                        doorHeaderUrl:that.userData.doorHeaderUrl
                    }
                    ajax.formRes(url, data).then(res => {
                        // success
                        that.$Message.success('新增数据成功,页面正在跳转')
                        that.hasSubmit = false
                            let argu = {
                            }
                            that.$router.push({
                                name: 'partyindex',
                                params: argu
                            })
                    }).catch(res => {
                        console.log('有错误信息')
                        // fail
                    })
                } else {
                    console.log('新增失败。。。')
                }
            })
        },
        goBack () {
            this.$router.push({
                name: 'partyindex'
            })
        },
        
    },
    created: function () {
        this.init()
    },
    activated: function () {
        //this.init()
    }
}
</script>
<style>
    .demo-upload-list-cover{
        display: none;
        position: absolute;
        top: 0;
        bottom: 0;
        left: 0;
        right: 0;
        background: rgba(0,0,0,.6);
    }
    .card-upload:hover .demo-upload-list-cover{
        display: block;
    }
    .demo-upload-list-cover i{
        color: #fff;
        font-size: 20px;
        cursor: pointer;
        margin: 0 2px;
    }
    .demo-spin-icon-load{
        animation: ani-demo-spin 1s linear infinite;
    }
    @keyframes ani-demo-spin {
        from { transform: rotate(0deg);}
        50%  { transform: rotate(180deg);}
        to   { transform: rotate(360deg);}
    }
    .demo-spin-col{
        height: 100px;
        position: relative;
        border: 1px solid #eee;
    }
</style>

